package com.liusany.myshop.entity;

import java.util.List;

import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.Table;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.google.common.collect.Lists;

/**
 * 权限组.
 * 
 * 注释见{@link Admin}.
 * 
 * @author calvin
 */
@Entity
@Table(name = "t_role")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Role extends BaseEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2660312482863734867L;
	private String name;
	private String description;
	private Boolean isSystem = false;
	private List<String> permissionList = Lists.newArrayList();

	public Role() {
	}

	public Role(Long id, String name) {
		this.id = id;
		this.name = name;
	}
	public Role(Long id, String name, Boolean isSystem) {
		this.id = id;
		this.name = name;
		this.isSystem = isSystem;
	}
	@Column(length = 50, nullable = false, unique = true)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(length=255)
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}


	public Boolean getIsSystem() {
		return isSystem;
	}

	public void setIsSystem(Boolean isSystem) {
		this.isSystem = isSystem;
	}

	@ElementCollection
	@CollectionTable(name = "t_role_authority", joinColumns = { @JoinColumn(name = "role_id") })
	@Column(name = "permission")
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public List<String> getPermissionList() {
		return permissionList;
	}

	public void setPermissionList(List<String> permissionList) {
		this.permissionList = permissionList;
	}

	/*@Transient
	public String getPermissionNames() {
		List<String> permissionNameList = Lists.newArrayList();
		for (String permission : permissionList) {
			permissionNameList.add(Permission.parse(permission).displayName);
		}
		return StringUtils.join(permissionNameList, ",");
	}

	@Transient
	public String getPermissionTree() {
		List<String> permissionNameList = Lists.newArrayList();
		for (String permission : permissionList) {
			permissionNameList.add(Permission.parse(permission).displayName);
		}
		return StringUtils.join(permissionNameList, ",");
	}*/
	
	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}
}
